package com.ryskoo.controller.customer;

import com.ryskoo.basicdata.industry.domain.IndustryPO;
import com.ryskoo.basicdata.industry.service.IIndustryService;
import com.ryskoo.common.constant.CustomerParamConstant;
import com.ryskoo.customer.vendor.domain.VendorPO;
import com.ryskoo.customer.vendor.service.IVendorService;
import com.ryskoo.framework.core.bean.Pager;
import com.ryskoo.framework.core.bean.QueryMessageDTO;
import com.ryskoo.framework.core.bean.ReturnCodeEnum;
import com.ryskoo.framework.core.bean.ReturnMessageDTO;
import com.ryskoo.framework.core.bean.SimpleDataParam;
import com.ryskoo.framework.core.controller.BasicHandler;
import com.ryskoo.framework.core.utils.CommonUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 商户管理 web controller
 *
 * @author hh
 */
@Controller("customerVendor")
@RequestMapping(value = "/vendor/customer/vendor")
public class VendorController extends BasicHandler<VendorPO> {

	private IVendorService vendorService;

	@Autowired
	private IIndustryService industryService;

	public VendorController() {
		super("system/customer/vendor", "Vendor");
	}

	@Autowired
	public void setService(IVendorService vendorService) {
		this.vendorService = vendorService;
		super.setService(vendorService);
	}

	@Override
	public String toView(@PathVariable(value = "id") String id, Model model, HttpServletRequest request) {
		VendorPO dto = vendorService.getByUuidExpand(id);
        model.addAttribute("dto", dto);
        return "system/customer/vendor/VendorView";
	}

	@Override
	public String toEdit(Model model, @PathVariable(value = "id") String id, HttpServletRequest request) {
		VendorPO dto = vendorService.getByUuidExpand(id);
		model.addAttribute("dto", dto);
		this.buildEditPageData(model, dto, request);
		return "system/customer/vendor/VendorEdit";
	}

	@Override
	public ReturnMessageDTO list(QueryMessageDTO queryMessage) {
		ReturnMessageDTO<List<VendorPO>> returnMessage = new ReturnMessageDTO<List<VendorPO>>();
		try {
			List<VendorPO> VendorList = vendorService.queryByConditionExpand(queryMessage, true);
			int count = vendorService.getCount(queryMessage);
			returnMessage.setReturnCode(ReturnCodeEnum.SUCCESS.getReturnCode());
			returnMessage.setReturnNote("查询成功");
			returnMessage.setReturnMessage(VendorList);
			returnMessage.setPager(new Pager(queryMessage.getPageShow(), queryMessage.getStartNum(), count));
		} catch (Exception e) {
			returnMessage.setReturnCode(ReturnCodeEnum.ERROR.getReturnCode());
			returnMessage.setReturnNote("查询商户列表失败");
			e.printStackTrace();
		}
		return returnMessage;
	}

	@Override
	protected void buildListPageData(Model model, HttpServletRequest request) {
		// 账号状态枚举
		List<SimpleDataParam> stateList = CommonUtil
				.converseSimpleDataParamMap(CustomerParamConstant.getCustomerActiveStateMap(), false);
		model.addAttribute("stateList", stateList);
		// 商户类型枚举
		List<SimpleDataParam> typeList = CommonUtil.converseSimpleDataParamMap(CustomerParamConstant.getVendorTypeMap(),
				false);
		model.addAttribute("typeList", typeList);
		// 所属行业列表
		List<IndustryPO> industryList = industryService.getAll();
		model.addAttribute("industryList", industryList);
		// 商户的默认密码
		model.addAttribute("defaultPassword", CustomerParamConstant.PASSWORD);
	}

	@Override
	protected void buildAddPageData(Model model, HttpServletRequest request) {
		// 账号状态枚举
		List<SimpleDataParam> stateList = CommonUtil
				.converseSimpleDataParamMap(CustomerParamConstant.getCustomerActiveStateMap(), false);
		model.addAttribute("stateList", stateList);
		// 商户类型枚举
		List<SimpleDataParam> typeList = CommonUtil.converseSimpleDataParamMap(CustomerParamConstant.getVendorTypeMap(),
				false);
		model.addAttribute("typeList", typeList);
		// 商户销售模式枚举
		List<SimpleDataParam> patternList = CommonUtil
				.converseSimpleDataParamMap(CustomerParamConstant.getVendorPatternMap(), false);
		model.addAttribute("patternList", patternList);
		// 所属行业列表
		List<IndustryPO> industryList = industryService.getAll();
		model.addAttribute("industryList", industryList);
		model.addAttribute("industryList", industryList);

	}

	@Override
	protected void buildEditPageData(Model model, VendorPO dto, HttpServletRequest request) {
		// 编辑的时候，会员密码要重置
		dto.setPassword(CustomerParamConstant.PASSWORD);
		// 账号状态枚举
		List<SimpleDataParam> stateList = CommonUtil
				.converseSimpleDataParamMap(CustomerParamConstant.getCustomerActiveStateMap(), false);
		model.addAttribute("stateList", stateList);
		// 商户类型枚举
		List<SimpleDataParam> typeList = CommonUtil.converseSimpleDataParamMap(CustomerParamConstant.getVendorTypeMap(),
				false);
		model.addAttribute("typeList", typeList);
		// 商户销售模式枚举
		List<SimpleDataParam> patternList = CommonUtil
				.converseSimpleDataParamMap(CustomerParamConstant.getVendorPatternMap(), false);
		model.addAttribute("patternList", patternList);
		// 所属行业列表
		List<IndustryPO> industryList = industryService.getAll();
		model.addAttribute("industryList", industryList);
	}
}
